package com.example.communitypro.service.impl;

import com.example.communitypro.mapper.CommentMapper;
import com.example.communitypro.mapper.PostMapper;
import com.example.communitypro.mapper.UserInformationMapper;
import com.example.communitypro.pojo.Comment;
import com.example.communitypro.pojo.Post;
import com.example.communitypro.pojo.UserInformation;
import com.example.communitypro.service.UserInformationService;
import com.example.communitypro.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UserInformationServiceImpl implements UserInformationService {
    @Autowired(required=false)
    private UserInformationMapper userInformationMapper;
    @Autowired(required=false)
    private PostMapper postMapper;
    @Autowired(required=false)
    private CommentMapper commentMapper;


    @Override
    public Result addUserInformation(UserInformation record){
        try {
            if(userInformationMapper.insertSelective(record)>0)
            {
                return Result.ok().message("新增用户信息成功");
            }
            else{
                return Result.error().message("新增用户信息失败");
            }
        }catch (Exception e){
            return Result.error().message("新增用户信息失败");
        }
    }


    @Override
    public Result dropUserInformation(int userNo){
        try {
            if(userInformationMapper.deleteByPrimaryKey(userNo)>0)
            {
                return Result.ok().message("删除用户信息成功");
            }
            else{
                return Result.error().message("删除用户信息失败");
            }
        }catch (Exception e){
            return Result.error().message("删除用户信息失败");
        }
    }

    @Override
    public Result modifyUserInformation(UserInformation record){
        try {
            if(userInformationMapper.updateByPrimaryKeySelective(record)>0)
            {
                return Result.ok().message("修改成功");
            }
            else{
                return Result.error().message("修改失败：未找到对应记录");
            }
        }catch (DuplicateKeyException e){
            System.err.println(e);
            return Result.error().message("修改失败：已经存在对应记录");
        }
        catch (Exception e){
            System.err.println(e);
            return Result.error().message("修改失败："+e.toString());
        }
    }

    @Override
    public Result queryUserInformation(int userNo){
        try {
            UserInformation userInformation=userInformationMapper.selectByPrimaryKey(userNo);
            if(userInformation!=null)
            {
                return Result.ok().message("查找用户信息成功").data("userInformation",userInformation).count(1);
            }
            else{
                return Result.error().message("查找用户信息失败");
            }
        }catch (Exception e){
            return Result.error().message("查找用户信息失败");
        }
    }

    @Override
    public UserInformation login(String user_account, String user_password){

            UserInformation userInformation=userInformationMapper.login(user_account,user_password);
            if(userInformation!=null) {
                return userInformation;
            }
            else{
                return null;
            }
    }

    @Override
    public Result selectUserByKey(String keyword){

        try {
            List<UserInformation> userInformations=userInformationMapper.selectUserByKey(keyword);
            if(userInformations!=null)
            {
                return Result.ok().message("模糊查询成功").data("userInformations",userInformations).count(userInformations.size());
            }
            else{
                return Result.error().message("模糊查询失败");
            }
        }catch (Exception e){
            return Result.error().message("模糊查询失败");
        }
    }

    @Override
    public Result queryPassUser(int user_status){

        try {
            List<UserInformation> userInformations=userInformationMapper.getPassUser(user_status);
            if(userInformations!=null)
            {
                return Result.ok().message("得到某状态下的用户成功").data("userInformations",userInformations).count(userInformations.size());
            }
            else{
                return Result.error().message("得到某状态下的用户失败");//
            }
        }catch (Exception e){
            return Result.error().message("得到某状态下的用户失败");
        }
    }

    @Override
    public int count() {
        return userInformationMapper.count();
    }

    @Override
    public Result findAllUser() {

        try {
            Map<Integer, UserInformation> users = userInformationMapper.getAllUser().parallelStream().collect(Collectors.toMap(UserInformation::getUserNo, v -> v));
            Map<Integer, Post> posts =  postMapper.getHotPost().parallelStream().collect(Collectors.toMap(Post::getPostNo, v -> v));
            Map<Integer, Comment> comments = commentMapper.getAllComment().parallelStream().collect(Collectors.toMap(Comment::getCommentNo, v -> v));
            return Result.ok().message("得到某状态下的用户成功").data("users",users).data("posts",posts).data("comments",comments);
        }catch (Exception e){
            return Result.error().message("得到某状态下的用户失败");
        }
    }

}
